package com.tiancheng.onecode.mq;

import com.alibaba.fastjson2.JSON;
import com.tiancheng.onecode.entity.MemberOrder;
import com.tiancheng.onecode.enums.OrderStatusEnum;
import com.tiancheng.onecode.exception.Asserts;
import com.tiancheng.onecode.service.IMemberOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author jiangjun
 * @description 取消订单消息的处理者
 * @date 2024/10/14 23:39
 */
@Component
@RabbitListener(queues = "oncode.order.cancel")
@Slf4j
public class CancelOrderReceiver {

    @Autowired
    private IMemberOrderService orderInfoService;

    @RabbitHandler
    public void handle(String paymentJSON) throws Exception {
        log.info("order cancel with timeout! receive msg paymentJSON:{}", paymentJSON);
        MemberOrder paymentInfo = JSON.parseObject(paymentJSON, MemberOrder.class);

        MemberOrder orderInfo = orderInfoService.queryByOrderId(paymentInfo.getOrderId());
        if (!orderInfo.getOrderStatus().equals(OrderStatusEnum.waiting_pay.getCode())) {
            // 已支付则返回处理成功
            return;
        }
        Boolean success = orderInfoService.cancelOrder(orderInfo.getOrderId());
        Asserts.isTrue(null != success && success, "取消失败");
    }
}
